
import org.apache.xerces.parsers.DOMParser;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.w3c.dom.Document;

/**
 * 
 */
/**
 * HoneyJS: A js virtual machine for jhoneyc
 *
 * @author Zhijie Chen
 */
public class HoneyJS {

    public Context cx;
    protected Scriptable scope;

    public HoneyJS(DOMParser parser) {
        // Creates and enters a Context. The Context stores information
        // about the execution environment of a script.        
        cx = Context.enter();

        // Initialize the standard objects (Object, Function, etc.)
        // This must be done before scripts can be executed. Returns
        // a scope object that we use in later calls.
        scope = cx.initStandardObjects();
        //Object wrappedOut2 = Context.javaToJS(parser, scope);
        //ScriptableObject.putProperty(scope, "parser", wrappedOut2);

        try {
            ScriptableObject.defineClass(scope, HoneyActiveX.class);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void evaluateBuffer(String scriptBuf) {
        // Now evaluate the string we've colected.
        Object result = cx.evaluateString(scope, scriptBuf, "<HoneyJS>", 1, null);
        // TODO: how to handle the result?
    }

    public void initializeDocument(Document document) {
        Object wrappedOut = Context.javaToJS(document, scope);
        ScriptableObject.putProperty(scope, "document", wrappedOut);

    }
}
